package com.qch.thread;

import com.qch.filter.TPMonitorFilter;
import com.qch.filter.TimeRecordDO;

import java.util.*;

/**
 * @Author qch
 * @Date 2021/2/20
 *
 */
public class Task implements Runnable {

    public void run() {
        System.out.println("====================开始汇报======================");
        Map<String, List<TimeRecordDO>> time_record_map = TPMonitorFilter.TIME_RECORD_Map;
        long cur=System.currentTimeMillis();
        for (Map.Entry<String, List<TimeRecordDO>> listEntry : time_record_map.entrySet()) {
            List<TimeRecordDO>effectiveList=new ArrayList<>();
            for (TimeRecordDO timeRecordDO : listEntry.getValue()) {
                if (timeRecordDO.getStartTime()>=(cur-60000)&&timeRecordDO.getStartTime()<cur){
                    effectiveList.add(timeRecordDO);
                }
            }
           doReport(listEntry.getKey(),effectiveList);
        }
        System.out.println("====================汇报结束======================");
    }

    private void doReport(String key, List<TimeRecordDO> effectiveList) {
        effectiveList.sort(TimeRecordDO::compareTo);
        int tp90Index=effectiveList.size()*90/100;
        TimeRecordDO tp90Record=effectiveList.get(tp90Index);
        int tp99Index=effectiveList.size()*99/100;
        TimeRecordDO tp99Record=effectiveList.get(tp99Index);
        System.out.println(key+"=》TP90:耗时"+tp90Record.getSpendTime()+"毫秒;TP99:耗时"+tp99Record.getSpendTime()+"毫秒");
    }

   /* public void run1() {
        Map<String, Map<Long, TreeMap<Long, Long>>> timeRecord = TPMonitorFilter.TIME_RECORD;
        for(String key:timeRecord.keySet()){
            //每个方法计算当前时间前一分钟的TP90,TP99
            //1.获取该方法当前时间前一分钟的TreeMap
            Map<Long, TreeMap<Long, Long>> sec_Map = timeRecord.get(key);
            TreeMap<Long, Long> spendTime_count_TreeMap=new TreeMap<>(Comparator.naturalOrder());
            long now=getSecMillis();
            long before60Sec=now-60*1000;
            //将不在一分钟内的时间删除
           *//* for (Long sec : sec_Map.keySet()) {
                if(sec<before60Sec)
                {
                    sec_Map.remove(sec);
                }
            }*//*
            for (Long sec : sec_Map.keySet()) {
                    if(sec>=before60Sec&&sec<=now)
                    {
                        if(spendTime_count_TreeMap.size()<=0){
                            spendTime_count_TreeMap.putAll(sec_Map.get(sec));
                        }else {
                            for (Long spendTime : spendTime_count_TreeMap.keySet()){
                                for (Long spendTime2 : sec_Map.get(sec).keySet()){
                                    if(sec_Map.get(sec).containsKey(spendTime))
                                    {
                                        long count=spendTime_count_TreeMap.get(spendTime)+sec_Map.get(sec).get(spendTime2);
                                        spendTime_count_TreeMap.put(spendTime,count);
                                    }else {
                                        spendTime_count_TreeMap.put(spendTime2,sec_Map.get(sec).get(spendTime2));
                                    }
                                }

                            }
                        }
                    }

            }
            doTask(spendTime_count_TreeMap,key);
            *//*if(min_Map.get(getrMinuteAgoMillis(1))==null){
                System.out.println("还在准备阶段。。。。");
            }else {
                spendTime_count_TreeMap = min_Map.get(getrMinuteAgoMillis(1));
                doTask(spendTime_count_TreeMap,key);
               *//**//* if(min_Map.containsKey(getrMinuteAgoMillis(2)))
                {
                    //前两分钟的内容已不需要在监控可删除，避免内存占用
                    min_Map.remove(getrMinuteAgoMillis(2));
                }*//**//*
            }*//*

        }
        

    }
*/
    private void doTask(TreeMap<Long,Long> spendTime_count_treeMap,String service) {
        Long total=spendTime_count_treeMap.values().stream().mapToLong(o->o).sum();
        Double idx90= Math.ceil((double)total*90/100);
        Double idx99= Math.ceil((double)total*99/100);
        Map.Entry<Long, Long> idx90Entry = spendTime_count_treeMap.floorEntry(idx90.longValue());
        Map.Entry<Long, Long> idx99Entry = spendTime_count_treeMap.floorEntry(idx99.longValue());
        if(idx90Entry==null){
            System.out.println(service+"=》TP90:耗时无法计算");
        }else {
            Long TP90_spendTime=idx90Entry.getKey();
            System.out.println(service+"=》TP90:"+TP90_spendTime);
        }
        if(idx90Entry==null){
            System.out.println(service+"=》TP99:耗时无法计算");
        }else {
        Long TP99_spendTime=idx99Entry.getKey();
        System.out.println(service+"=》TP99:"+TP99_spendTime);
        }
    }

    private long getrMinuteAgoMillis(int min)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(System.currentTimeMillis());
        // 将秒,毫秒域清零
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        return cal1.getTimeInMillis()-min*60*1000;
    }
    private long getSecMillis()
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTimeInMillis(System.currentTimeMillis());
        // 将毫秒域清零
        cal1.set(Calendar.MILLISECOND, 0);
        return cal1.getTimeInMillis();
    }
}
